import { Component, Input, OnChanges } from '@angular/core'

import { IBiz } from '@shared/interfaces/biz'
import { IBizInfoRow } from '@shared/interfaces/baseInfo'
import { EBizLevel } from '@shared/enums'
import { I18NService } from '@core'

@Component({
  selector: 'app-aams-info',
  templateUrl: './aams-info.component.html',
  styleUrls: ['./aams-info.component.less'],
})
export class AamsInfoComponent implements OnChanges {
  @Input() info: IBiz
  @Input() level: EBizLevel

  columns: IBizInfoRow[]
  constructor(
    private i18n: I18NService,
  ) {}

  ngOnChanges() {
    if (!this.info) return
    this.columns = this.generateColumns(this.info)
  }

  generateColumns(input: IBiz): IBizInfoRow[] {
    const columns: IBizInfoRow[] = []
    if (!input) {
      return columns
    }
    if (input.insCode) {
      columns.push(this.acquirerRow(input))
    }

    if (input.agentCode && this.level !== EBizLevel.Group) {
      columns.push(this.groupRow(input))
    }

    if (input.intMerCode && this.level !== EBizLevel.Merchant) {
      columns.push(this.merchantRow(input))
    }

    if (input.intStoreCode && this.level !== EBizLevel.Store) {
      columns.push(this.storeRow(input))
    }

    return columns
  }

  storeRow(input: IBiz): IBizInfoRow {
    return {
      img: './assets/images/centaur/store.png',
      title: this.i18n.fanyi('GENERAL.BIZ_LEVEL.STORE'),
      columns: [
        {
          span: 8,
          index: 'storeCode',
          label: this.i18n.fanyi('GENERAL.STORE.NUM'),
          value: input.storeNum,
        },
        {
          span: 8,
          index: 'storeName',
          label: this.i18n.fanyi('GENERAL.STORE.NAME'),
          value: input.storeName,
        },
        {
          span: 8,
          index: 'storeNameEN',
          label: this.i18n.fanyi('GENERAL.STORE.NAME_EN'),
          value: input.storeNameEN,
        },
      ],
    }
  }

  merchantRow(input: IBiz): IBizInfoRow {
    return {
      img: './assets/images/centaur/merchant.png',
      title: this.i18n.fanyi('GENERAL.BIZ_LEVEL.MERCHANT'),
      columns: [
        {
          span: 8,
          index: 'merCode',
          label: this.i18n.fanyi('GENERAL.MERCHANT.NUM'),
          value: input.merNum,
        },
        {
          span: 8,
          index: 'merName',
          label: this.i18n.fanyi('GENERAL.MERCHANT.NAME'),
          value: input.merName,
        },
        {
          span: 8,
          index: 'merNameEN',
          label: this.i18n.fanyi('GENERAL.MERCHANT.NAME_EN'),
          value: input.merNameEN,
        },
      ],
    }
  }

  groupRow(input: IBiz): IBizInfoRow {
    return {
      img: './assets/images/centaur/agent.png',
      title: this.i18n.fanyi('GENERAL.BIZ_LEVEL.AGENT'),
      columns: [
        {
          span: 8,
          index: 'agentCode',
          label: this.i18n.fanyi('GENERAL.AGENT.CODE'),
          value: input.agentCode,
        },
        {
          span: 8,
          index: 'agentName',
          label: this.i18n.fanyi('GENERAL.AGENT.NAME'),
          value: input.agentName,
        },
        {
          span: 8,
          index: 'agentNameEN',
          label: this.i18n.fanyi('GENERAL.AGENT.NAME_EN'),
          value: input.agentNameEN,
        },
      ],
    }
  }

  acquirerRow(input: IBiz): IBizInfoRow {
    return {
      img: './assets/images/centaur/acquirer.png',
      title: this.i18n.fanyi('GENERAL.BIZ_LEVEL.INSTITUTION'),
      columns: [
        {
          span: 8,
          index: 'insCode',
          label: this.i18n.fanyi('GENERAL.INSTITUTION.CODE'),
          value: input.insCode,
        },
        {
          span: 8,
          index: 'insName',
          label: this.i18n.fanyi('GENERAL.INSTITUTION.NAME'),
          value: input.insName,
        },
        {
          span: 8,
          index: 'insNameEN',
          label: this.i18n.fanyi('GENERAL.INSTITUTION.NAME_EN'),
          value: input.insNameEN,
        },
      ],
    }
  }
}
